package com.yager.controller;

import com.yager.model.Collection;
import com.yager.model.House;
import com.yager.model.User;
import com.yager.service.CollectionService;
import com.yager.service.HouseService;
import com.yager.util.FilesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.mail.Session;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/collection")
public class CollectionController {

    @Autowired
    private CollectionService collectionService;

    @Autowired
    private HouseService houseService;

    @ResponseBody
    @RequestMapping("/addHouseCollection")
    public void addHouseCollection(HttpSession session, HttpServletRequest req) {
        System.out.println("请求发送成功");
        User user = (User) session.getAttribute("user");
        House house = (House) session.getAttribute("Onehouse");
//        String houseid=req.getParameter("houseid");

        collectionService.addNewCollection(user.getUserid(), house.getHouseid());
        session.setAttribute("collected","已收藏");

    }

    @ResponseBody
    @RequestMapping("/removeHouseCollection")
    public void removeHouseCollection(HttpSession session,HttpServletRequest req){
        User user=(User) session.getAttribute("user");
        House house=(House) session.getAttribute("Onehouse");
////        String houseid=req.getParameter("houseid");
        collectionService.removeCollection(user.getUserid(), house.getHouseid());
        session.setAttribute("collected","收藏");
    }

    @RequestMapping("/toShowCollection")
    public String getHouseCollectionByUserId(HttpSession session){
        User user=(User) session.getAttribute("user");
        List<Collection> list = collectionService.getHouseCollectionByUserId(user.getUserid());
        if(list == null||list.size()==0) return "collection/noCollection";
        session.setAttribute("houseCollectionList",list);
        List<House> houseList = new ArrayList<House>();
        for(int i = 0 ; i < list.size(); i++){
            House house = houseService.selectHouseByPrimaryKey(list.get(i).getHouseid());
            houseList.add(house);
        }
        List<String> imageList = new ArrayList<String>();
        for (int i = 0; i < houseList.size(); i++) {
            House house = houseList.get(i);
            String dirLocalPath = house.getHouseimages();
            File[] files = FilesUtil.getAllFile(dirLocalPath);
            String a = files[0].getPath();
            a = a.substring(a.lastIndexOf("h"),a.length());
            imageList.add(a);
        }
        session.setAttribute("houses", houseList);
        session.setAttribute("images", imageList);

        return "collection/showCollection";
    }

}



//
//
//
//    @RequestMapping("/addOrderCollection")
//    public void addOrderCollection(HttpSession session, HttpServletRequest req){
//        System.out.println("请求发送成功");
//        User user=(User) session.getAttribute("user");
//        String orderid=req.getParameter("orderid");
////        String houseid=req.getParameter("houseid");
//        collectionService.addNewCollection(user.getUserid(),orderid);
////        return "#";
//    }
//
//
//    @RequestMapping("/removeOrderCollection")
//    public void removeOrderCollection(HttpSession session,HttpServletRequest req){
//        User user=(User) session.getAttribute("user");
//        String orderid=req.getParameter("orderid");
////        String houseid=req.getParameter("houseid");
//        collectionService.removeCollection(user.getUserid(),orderid);
////        return "#";
//    }
//
//    @RequestMapping("/getOrderCollectionByUserId")
//    public String getOrderCollectionByUserId(HttpSession session){
//        User user=(User) session.getAttribute("user");
//        List<Collection> list = collectionService.getOrderCollectionByUserId(user.getUserid());
//        session.setAttribute("orderCollectionList",list);
//        return "#";
//    }
//
//}
